package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.repository.IWendaAnswerCommentRepository;
import cn.wolfcode.luowowo.common.domain.WendaAnswerComment;
import cn.wolfcode.luowowo.common.query.WendaAnswerCommentQuery;
import cn.wolfcode.luowowo.common.service.IWendaAnswerCommentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class WendaAnswerCommentServiceImpl implements IWendaAnswerCommentService {
    @Autowired
    private IWendaAnswerCommentRepository repository;
    @Autowired
    private MongoTemplate template;
    @Reference
    private IWendaAnswerCommentService wendaAnswerCommentService;

    @Override
    public void saveOrUpdate(WendaAnswerComment wendaAnswerComment) {
        if (!StringUtils.hasLength(wendaAnswerComment.getId())){
            wendaAnswerComment.setCreateTime(new Date());
        }
        repository.save(wendaAnswerComment);
    }

    @Override
    public Page query(WendaAnswerCommentQuery qo) {
        //查询条件
        Query query = new Query();
        Criteria criteria = new Criteria();
        //回答id条件
        if (qo.getQuestionId()!=-1){
            query.addCriteria(criteria.and("questionId").is(qo.getQuestionId()));
        }
        long count = template.count(query, WendaAnswerComment.class);
        if (count==0){
            return Page.empty();
        }
        //参数1:当前页  注意从0开始
        //参数2:每页显示条数
        //参数3:排序规则  第一个参数表示倒序  第二个参数表示按哪个字段排序
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);
        List<WendaAnswerComment> list = template.find(query, WendaAnswerComment.class);
        return new PageImpl(list,pageable,count);
    }

    //判断是点赞还是取消
    @Override
    public boolean thumbUp(String aid, Long userId) {
        Optional<WendaAnswerComment> op = repository.findById(aid);
        WendaAnswerComment comment = op.get();
        if(comment!=null){
            List<Long> list = comment.getThumbuplist();
            if (list.contains(userId)) {
                //集合里已经有了的话表示取消点赞
                comment.setThumbupnum(comment.getThumbupnum()-1);
                //移除已点赞id集合里的用户id
                list.remove(userId);
            }else {
                //集合里没有的话表示点赞操作
                comment.setThumbupnum(comment.getThumbupnum()+1);
                //把用户id添加在已点赞id集合里
                list.add(userId);
            }
            //更新操作
            this.saveOrUpdate(comment);
            return true;
        }
        return false;
    }

    @Override
    public WendaAnswerComment get(String aid) {
        Optional<WendaAnswerComment> op = repository.findById(aid);
        return op.get();
    }

    @Override
    public boolean thumbsupnumIncrease(String aid, Long uid) {
        Optional<WendaAnswerComment> op = repository.findById(aid);
        WendaAnswerComment comment = op.get();
        List<Long> list = comment.getThumbuplist();
       if (list.contains(uid)){
           return false;
       }else {
           comment.setThumbupnum(comment.getThumbupnum()+1);
           list.add(uid);
       }
        repository.save(comment);
        return true;
    }

    @Override
    public WendaAnswerComment getByid(String aid) {
        Optional<WendaAnswerComment> op = repository.findById(aid);
        WendaAnswerComment comment = op.get();
        return comment;
    }


}
